#include<iostream>
#include<string>
#include<vector>
using namespace std;


class Solution {
public:
    int islandPerimeter(vector<vector<int>>& grid) {
        int n = grid.size();
        int m = grid[0].size();
        int ans = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                int sum = 0;
                if (grid[i][j]) {
                    if (i - 1 < 0 || !grid[i - 1][j]) {
                        sum = sum + 1;
                    }
                    if (i + 1 >= n || !grid[i + 1][j]) {
                        sum = sum + 1;
                    }
                    if (j + 1 >= m || !grid[i][j + 1]) {
                        sum = sum + 1;
                    }
                    if (j - 1 < 0 || !grid[i][j - 1]) {
                        sum = sum + 1;
                    }

                }
                ans += sum;
            }
        }
        return ans;
    }
};

//
//int main() {
//    vector<vector<int>> v;
//    /*vector<int> p1;
//    p1 = { 0,1,0,0 };
//    vector<int> p2 = {1,1,1,0};
//    vector<int> p3 = { 0,1,0,0 };
//    vector<int> p4 = {1,1,0,0};
//    v.push_back(p1);
//    v.push_back(p2);
//    v.push_back(p3);
//    v.push_back(p4);*/
//
//    v = { {0,1,0,0},{1,1,1,0},{0,1,0,0},{1,1,0,0} };
//    
//    Solution S;
//    int ant = S.islandPerimeter(v);
//    cout << ant << endl;
//
//}

